package com.example.demo.service;

import com.example.demo.entity.GeneGroup;
import com.example.demo.entity.Staff;
import com.example.demo.entity.Store;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class GaServiceImpl implements GaService{

    public void testGa(){
        Staff staff1 = new Staff("1", null);
        Staff staff2 = new Staff("2", null);
        Staff staff3 = new Staff("3", null);
        Staff staff4 = new Staff("4", null);
        Staff staff5 = new Staff("5", null);
        Staff staff6 = new Staff("6", null);
        //创建商铺
        Store store = new Store();
        store.addStaff(staff1);
        store.addStaff(staff2);
        store.addStaff(staff3);
        store.addStaff(staff4);
        store.addStaff(staff5);
        store.addStaff(staff6);
        //创建客流模型
        int[] customers1 = new int[]{
                4, 4, 3, 4, 6, 7, 8, 7, 6, 5, 4, 7, 8, 9, 6, 5, 4, 4, 4, 4, 4, 6, 7, 6, 0, 0
        };
//        int[] customers2 = new int[]{
//                4, 4, 3, 4, 6, 7, 8, 7, 6, 5, 4, 7, 8, 9, 6, 5, 4, 4, 4, 4, 4, 6, 7, 6, 0, 0
//        };int[] customers3 = new int[]{
//                4, 4, 3, 4, 6, 7, 8, 7, 6, 5, 4, 7, 8, 9, 6, 5, 4, 4, 4, 4, 4, 6, 7, 6, 0, 0
//        };int[] customers4 = new int[]{
//                4, 4, 3, 4, 6, 7, 8, 7, 6, 5, 4, 7, 8, 9, 6, 5, 4, 4, 4, 4, 4, 6, 7, 6, 0, 0
//        };int[] customers5 = new int[]{
//                4, 4, 3, 4, 6, 7, 8, 7, 6, 5, 4, 7, 8, 9, 6, 5, 4, 4, 4, 4, 4, 6, 7, 6, 0, 0
//        };int[] customers6 = new int[]{
//                4, 4, 3, 4, 6, 7, 8, 7, 6, 5, 4, 7, 8, 9, 6, 5, 4, 4, 4, 4, 4, 6, 7, 6, 0, 0
//        };int[] customers7 = new int[]{
//                4, 4, 3, 4, 6, 7, 8, 7, 6, 5, 4, 7, 8, 9, 6, 5, 4, 4, 4, 4, 4, 6, 7, 6, 0, 0
//        };
        int[] expertNeedStuff = store.generateExpertNeedStuffList(customers1, 1);
        for (int i = 0; i < expertNeedStuff.length; i++) {
            System.out.print(expertNeedStuff[i]);
        }
        System.out.println("----");
//        store.generateExpertNeedStuffList(customers2, 1);
//        store.generateExpertNeedStuffList(customers3, 2);
//        store.generateExpertNeedStuffList(customers4, 3);
//        store.generateExpertNeedStuffList(customers5, 4);
//        store.generateExpertNeedStuffList(customers6, 5);
//        store.generateExpertNeedStuffList(customers7, 6);
//        创建基因组
        GeneGroup geneGroup = new GeneGroup(store);
        int fixTime = 0;//适应次数
        //记录当前商铺的员工值班表 记录
        double fix = Integer.MAX_VALUE;
        GeneGroup tmp = new GeneGroup();
        ArrayList<Double> list = new ArrayList<>();
        while (fixTime < 10) {
            geneGroup.fitness(geneGroup, 1,expertNeedStuff);
            fixTime++;
            list.add(geneGroup.getFitnessValue());
            if (geneGroup.getFitnessValue() < fix) {
                tmp = geneGroup;
                fix = geneGroup.getFitnessValue();
            }
            geneGroup = new GeneGroup(store);
        }
        store = tmp.getStore();
        for (int i = 0; i < store.staffList.size(); i++) {
            System.out.println(store.staffList.get(i).getStaffName());
            for (int j = 0; j < store.staffList.get(i).gene.chromoSome.get(1).length; j++) {
                System.out.print(store.staffList.get(i).gene.chromoSome.get(1)[j]);
            }
            System.out.println("------------------");
        }
        ArrayList<int[]> workTimeCount = tmp.getWorkTimeCount();
        for (int i = 0; i < workTimeCount.get(1).length; i++) {
            System.out.print(workTimeCount.get(1)[i]);
        }
        System.out.println("---------------");

    }
}
